using System.Collections;
using System.Collections.Generic;
using Microsoft.Unity.VisualStudio.Editor;
using UnityEngine;
using UnityEngine.UI;
using Image = UnityEngine.UI.Image;

public enum ToolType
{
    None,
    Hook,
    Net
}

public class PlayerController : Singleton<PlayerController>
{
    public Joystick joystick;
    public float moveSpeed = 5f;
    private bool isStretching = false;
    public GameObject body;
    
    [Header("Tool Prefabs")]
    public GameObject fishHookPrefab;
    public GameObject fishNetPrefab;
    
    [Header("Settings")]
    public Transform toolHolder;
    
    private int _currentCycleIndex = -1; 
    private GameObject _currentToolObject = null;
    private ToolType _currentToolType = ToolType.None;

    public Body bo;
    public List<ToolType> toolCycleOrder = new List<ToolType>
    {
        ToolType.Hook,
        ToolType.Net, 
    };

    
    void Start()
    {
        if (toolCycleOrder.Count > 0)
        {
            _currentCycleIndex = 0; // 指向列表的第一个元素
            SwitchTool(toolCycleOrder[_currentCycleIndex]);
        }
        else
        {
            Debug.LogWarning("Tool cycle order is empty! No tools to switch.");
        }
    }
    
    private void Update()
    {
        float horizontalInput = joystick.Horizontal;
        Vector3 movement = new Vector3(horizontalInput, 0f, 0f) * moveSpeed * Time.deltaTime;
        transform.Translate(movement);
        //todo 两个次数
        if (isStretching)
        {
            Body.Instance.Down();
        }

    }
    

    public void CycleNextTool()
    {
        if (Joystick.IsMoving) return;
        
        if (toolCycleOrder.Count == 0)
        {
            Debug.LogWarning("No tools available to cycle.");
            return;
        }
        _currentCycleIndex++;
        if (_currentCycleIndex >= toolCycleOrder.Count)
        {
            _currentCycleIndex = 0;
        }
        SwitchTool(toolCycleOrder[_currentCycleIndex]);
    }

    public void SwitchTool(ToolType toolToSwitchTo)
    {
        if (toolToSwitchTo == _currentToolType)
        if (FishTool.Instance != null)
        {
            if (toolToSwitchTo == ToolType.Hook && FishTool.Instance is FishHook) { /* Debug.Log("Already Hook."); */ return; }
            if (toolToSwitchTo == ToolType.Net && FishTool.Instance is FishNet) { /* Debug.Log("Already Net."); */ return; }
        }

        if (_currentToolObject != null)
        {
            FishTool.Instance.OnDestroy();
            Destroy(_currentToolObject);
            _currentToolObject = null;
        }
        else if (FishTool.Instance != null)
        {
            FishTool.Instance.OnDestroy();
            Destroy(FishTool.Instance.gameObject);
            print("销毁");
        }
        _currentToolType = toolToSwitchTo;
        GameObject prefabToInstantiate = null;
        switch (toolToSwitchTo)
        {
            case ToolType.Hook:
                prefabToInstantiate = fishHookPrefab;
                break;
            case ToolType.Net:
                prefabToInstantiate = fishNetPrefab;
                break;
            default:
                Debug.LogWarning("Unknown tool type selected: " + toolToSwitchTo);
                return;
        }
        if (prefabToInstantiate != null)
        {
            print("创建");
            _currentToolObject = Instantiate(prefabToInstantiate, toolHolder);
            //pc.fishHook = _currentToolObject;
            bo.ha = _currentToolObject;
            if (toolHolder != null)
            {
                _currentToolObject.transform.SetParent(toolHolder, worldPositionStays: false);
            }
            if (FishTool.Instance != null)
            {
                Debug.Log($"Switched to {FishTool.Instance.GetToolName()}.");
            } else {
                Debug.LogError($"Failed to switch to {toolToSwitchTo}. Instance is null after instantiation.");
            }
        }
    }
    
    //todo 回到原位开启
    public void ToggleSlide()
    {
        isStretching = !isStretching;
        if (isStretching)
        {
            FishTool.Instance.KaiQi();
            if (_currentToolType == ToolType.Hook)
            {
                InventoryManager.Instance.UseHook();
            }
            else if(_currentToolType == ToolType.Net)
            {
                InventoryManager.Instance.UseNet();
            }
        }
    }
}


